"""
Case Type   : 系统内部使用工具
Case Name   : 创建表包含各种数据类型
Create At   : 2022/03/03
Owner       : zou_jialiang050
Description :
    1.构建数据
    2.清理环境
Expect      :
    1.构建成功
    2. 成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.Common import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

Constant = Constant()


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pricomsh = CommonSH("PrimaryDbUser")
        self.primary_node = Node('PrimaryDbUser')
        self.target_path = os.path.join(macro.DB_BACKUP_PATH, "source_data")

    def test_ustore(self):
        text = f"----step1:创建表包含各种数据类型:expect:成功;----"
        self.log.info(text)
        file_lis = ['tab_ap_join_07.txt']
        for file in file_lis:
            self.common.scp_file(self.primary_node, f"{file}", self.target_path)

        sql1 = f"""drop table if exists tab_ap_join_07;
        create table tab_ap_join_07 (
                    c_int integer , 
                    c_smallint smallint , 
                    c_bigint bigint , 
                    c_decimal decimal , 
                    c_numeric numeric , 
                    c_real real , 
                    c_double double precision , 
                    c_money  varchar(100) , 
                    c_character_varying character varying(1024) , 
                    c_varchar varchar(20) , 
                    c_char char(1024) , 
                    c_text varchar(2000) , 
                    c_bytea bytea , 
                    c_timestamp_without  varchar(100) , 
                    c_timestamp_with varchar(100), 
                    c_boolean boolean ,  
                    c_oid  varchar(100) ,  
                    c_character character(10) , 
                    c_interval varchar(100) , 
                    c_date date , 
                    c_time_without varchar(100), 
                    c_time_with varchar(100),
                    c_binary_integer binary_integer,
                    c_binary_double binary_double,
                    c_dec decimal(2,1),
                    c_numeric_1 numeric(3,2),
                    c_varchar2 varchar2(100))  with (storage_type=ustore);
        
        copy tab_ap_join_07 from 
        '{os.path.join(self.target_path, 'tab_ap_join_07.txt')}' delimiters  ',';
        """

        res1 = self.pricomsh.execut_db_sql(sql1)
        self.log.info(res1)

        self.assertIn('CREATE TABLE', res1, 'sql1执行失败')
        self.assertIn('COPY 3000', res1, 'sql1执行失败')

    def tearDown(self):
        text = f"----step2:清理环境:expect:成功;----"
        self.log.info(text)
        del_flag = False
        del_cmd = f'rm -rf {self.target_path};' \
                  f'if [ -d {self.target_path} ]; ' \
                  f'then echo "exists";else echo "not exists"; fi'
        for num in range(10):
            del_res = self.common.get_sh_result(self.primary_node, del_cmd)
            if 'not exists' in del_res:
                del_flag = True
                break
        self.assertTrue(del_flag, '文件删除失败')
        self.log.info(f'----{os.path.basename(__file__)} finish----')

